<?php
/**
 * I-Cafe framework
 *
 * @category CF
 * @package CF
 * @link http://www.i-cafe.org
 * @license http://www.i-cafe.org/license
 * @version 04-04-2010
 * @author tcl_java@yahoo.com
 */

class CF_Loader {
    /**
     * @var array
     */
    protected static $_includePath = array();

    /**
     *
     * @param string|array $path
     * @return void
     */
    public static function addIncludePath($path) {
        self::restoreIncludePath();
        if(!is_array($path)) {
            $path = array($path);
        }

        foreach($path as $v) {
            if(isset(self::$_includePath[$v])) continue;
            self::$_includePath[$v] = $v;
        }

        self::restoreIncludePath();
        $includePath[] = get_include_path();
        $includePath = array_merge($includePath, self::$_includePath);
        set_include_path(implode(PATH_SEPARATOR, $includePath));
    }

    /**
     *
     * @param string|array $path
     * @return void
     */
    public static function removeIncludePath($path) {
        if(is_array($path)) {
            foreach($path as $v) {
                unset(self::$_includePath[$v]);
            }
        }else {
            unset(self::$_includePath[$path]);
        }

        self::restoreIncludePath();
        $includePath[] = get_include_path();
        $includePath = array_merge($includePath, self::$_includePath);
        set_include_path(implode(PATH_SEPARATOR, $includePath));
    }

    /**
     *
     * @return void
     */
    public static function restoreIncludePath() {
        restore_include_path();
    }

    /**
     * Load a file
     *
     * @param string $fileName
     * @param boolean $once [optional] default to false.
     * @return boolean
     * @throws CF_Loader_Exception
     */
    public static function loadFile($fileName, $once = false) {
        # check file name
        if(preg_match('/[^a-z0-9\\/\\\\_.:-]/i', $fileName)) {
            throw new CF_Exception('Illegal character in filename');
        }

        $res = $once? include_once $fileName : include $fileName;
        return $res;
    }

    /**
     * Load a PHP class
     * @param string $className
     * @return void
     * @throws CF_Loader_Exception
     */
    public static function loadClass($className) {
        if(class_exists($className, false) || interface_exists($className, false)) return true;

        $file = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
        self::loadFile($file);

        if(!class_exists($className, false) && !interface_exists($className, false)) {
            require_once 'CF/Exception.php';
            throw new CF_Exception("Class \"$className\" was not found");
        }
    }

    /**
     * Register autload
     * @return boolean
     */
    public static function registerAutoload() {
        return spl_autoload_register(array(__CLASS__, 'loadClass'));
    }
}
